package cn.xhc.common.util;

import java.io.IOException;
import java.util.Iterator;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class YxlSpiderUtil {
	/**
	 * 抓取测试项的列表页面
	 * 相当于栏目页面
	 * @param column 栏目
	 * @result 
	 * {"code":"0","info":"","data":{"totalPage":"28","itemsList":[{"name":"","url":"","imgPath":"","testedNum":"","favNum":"","pubTime":"","suggNum":"","questionNum":""}]}}
	 */
	public static  JSONObject itemList(String column,int page){
		JSONObject resultJson = new JSONObject();
		/* 抓取的Url */
		String urlStr = "http://www.xinli001.com/ceshi/" + column + "?page=" + page;
		/*使用  jsoupgju*/
		try {
			/*创建连接,请求服务器*/
			Connection connection = Jsoup.connect(urlStr);
			/*连接获取Doucment对象 == js的Document*/
			Document document = connection.get();
			/*获取数据*/
			Elements listShowEles = document.getElementsByClass("list_show");
			/*获取第一个list_show*/
			Element listShowEle = listShowEles.get(0);
			/*获取list+show下的fixed*/
			Elements fixedELes = listShowEle.getElementsByClass("fixed");
			ContantInfoUtil.LOGGER.info("-list_show的个数:{},fixed的个数:{}",listShowEles.size(),
					fixedELes.size());
			/*遍历结果*/
			JSONArray itemsArr = new JSONArray();
			for (Iterator iterator = fixedELes.iterator(); iterator.hasNext();) {
				Element firedEle = (Element) iterator.next();
				JSONObject itemsJson = new JSONObject();
				/*获取测试项的数据*/
				Element infoEle = firedEle.getElementsByClass("linfo fl").get(0);
				/*获取a标签*/
				Element aEle = infoEle.getElementsByTag("a").get(0);
				/*遍历a标签的属性和值*/
				String name = aEle.text();
				String href = aEle.attr("href");
				/*获取id作为outId*/
				//href="http://www.xinli001.com/ceshi/177"
				String id = href.substring(href.lastIndexOf("/")+1);
				/*获取图片的地址*/
				Element pboxEle = firedEle.getElementsByClass("pbox fl").get(0);
				Element imgEle = pboxEle.getElementsByTag("img").get(0);
				String imgPath = imgEle.attr("src");
				/*调用测试详情连接获取测试详情信息*/
				JSONObject itemInfoJson = itemInfo(href);
				if("0".equalsIgnoreCase(itemInfoJson.getString("code"))){
					itemsJson = itemInfoJson.getJSONObject("data");
//					ContantInfoUtil.LOGGER.info("--结果:{}",itemsJson.toString());
					
				}
				itemsJson.put("name",name);
				itemsJson.put("url",href);
				itemsJson.put("imgPath",imgPath);
				itemsJson.put("outId",id);
//				ContantInfoUtil.LOGGER.info("--结果:{}",itemsJson.toString());
				itemsArr.add(itemsJson);
			}
			JSONObject dataJson = new JSONObject();
			dataJson.put("itemsList",itemsArr);
			/*总页数*/
			Elements pageEles = document.getElementById("pages").getElementsByTag("a");
			/*取倒数第二个元素*/
			Element totalPageEle = pageEles.get(pageEles.size()-2);
			/*总页数*/
			dataJson.put("totalPage",totalPageEle.text());
			resultJson.put("code","0");
			resultJson.put("info","成功");
			resultJson.put("data",dataJson);
		} catch(Exception e) {
			resultJson.put("code","1");
			resultJson.put("info","报错了"+e);
			ContantInfoUtil.LOGGER.error("抓取数据失败,检查url或网络{},异常:{}",urlStr,e);
		}
		return resultJson;
	}
	/**
	 * 取测试项的详情
	 * @result 
	 * {"code":"0","info":"","data":{"testedNum":"","favNum":"","pubTime":"","suggNum":"","questionNum":""}}
	 */
	private static JSONObject itemInfo(String urlStr) {
		JSONObject itemInfoJson = new JSONObject();
		/*创建连接*/
		Connection connection = Jsoup.connect(urlStr);
		/*创建Document对象*/
		try {
			Document document = connection.get();
			/*解析测试项的内容*/
			Element tinfoEle = document.getElementsByClass("tinfo fl").get(0);
			Element suggEle = tinfoEle.getElementsByTag("a").get(0);
			/*评论数*/
			String suggNum = suggEle.text().substring(0, suggEle.text().indexOf("条"));
			Element totalEle = tinfoEle.getElementsByClass("total").get(0);
			Element questionEle = totalEle.getElementsByClass("fgreen fb").get(0);
			/*问题数*/
			String questionNum = questionEle.getElementsByTag("span").get(0).text();
			/*测试项的描述*/
			String content = document.getElementsByClass("tdesc").get(0).text();
			JSONObject dataJson = new JSONObject();
			dataJson.put("suggNum",suggNum);
			dataJson.put("questionNum",questionNum);
			dataJson.put("content",content);
			itemInfoJson.put("data",dataJson);
			itemInfoJson.put("code","0");
			itemInfoJson.put("info","成功");
		} catch (IOException e) {
			itemInfoJson.put("code","1");
			itemInfoJson.put("info","报错了"+e);
			ContantInfoUtil.LOGGER.error("创建连接失败,检查url或网络状况:{},{}",urlStr,e);
		}
		return itemInfoJson;
	}

}
